package com.ldc.wandroid.ui.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.ldc.common.model.BaseModel;
import com.ldc.common.mvvm.BaseMvvmActivity;
import com.ldc.common.utils.StringUtils;
import com.ldc.common.utils.ToastUtils;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.wandroid.databinding.ActivityAddPrivateArticleBinding;
import com.ldc.wandroid.viewmodel.AddSharedArticleViewModel;

public class AddPrivateArticleActivity extends BaseMvvmActivity<ActivityAddPrivateArticleBinding, AddSharedArticleViewModel> {


    public static void actionStart(Activity activity) {
        Intent intent = new Intent(activity, AddPrivateArticleActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }

    @Override
    protected ActivityAddPrivateArticleBinding initViewBinding() {
        return ActivityAddPrivateArticleBinding.inflate(getLayoutInflater());
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewModel.getResult().removeObserver(this::articleAdd);
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewModel.getResult().observeForever(this::articleAdd);
        viewBinding.btnSubmit.setOnClickListener(v -> {
            submit();
        });

    }

    private void submit() {
        String link = viewBinding.etLink.getText().toString();
        String title = viewBinding.etTitle.getText().toString();
        if (StringUtils.isEmpty(title) || StringUtils.isEmpty(link)) {
            ToastUtils.showShort("非法数据");
            return;
        }
        viewBinding.stateView.showLoadingView("提交数据，请等待");
        viewModel.userArticleAdd(title, link);
    }


    @Override
    protected AddSharedArticleViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, AddSharedArticleViewModel.class);
    }


    private void articleAdd(BaseModel<Object> result) {
        viewBinding.stateView.showContentView();

    }
}
